package com.kanq.clockin.service.inpl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.kanq.clockin.entity.User;
import com.kanq.clockin.service.IClockService;
import com.kanq.clockin.utils.NumRandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.HttpCookie;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class ClockImpl implements IClockService {
    @Override
    public User getUserInfo(String code) {
        User user = null;
        switch (code){
            case "hjw":
                user = User.HJW;
            break;
            default:
        }
        return user;
    }

    @Override
    public String actionClock(User user) {
        log.info("当前用户信息:{}", user.toString());
        HttpResponse execute = HttpRequest.get("http://erp.kqgeo.com:889/cops_User_login.action?name="+user.getName()+"&pswd="+user.getPassword()).execute();
        List<HttpCookie> cookies = execute.getCookies();
        HashMap<String, Object> map = new HashMap<>();
        map.put("lon", "114.39941" + NumRandomUtil.getRandomIntNum());
        map.put("lat", "30.45325" + NumRandomUtil.getRandomIntNum());
        map.put("address", "湖北省武汉市江夏区大学园路18号领航园");
        map.put("province", "湖北省");
        map.put("city", "武汉市");
        map.put("region", "江夏区");
        map.put("county", "");
        map.put("phoneSn", user.getPhoneSn());
        map.put("phoneIp", user.getPhoneIp());
        map.put("remark", "");
        map.put("def1", "144.85266" + NumRandomUtil.getRandomIntNum());
        map.put("def2", "湖北省武汉市江夏区大学园路18号领航园");
        map.put("def3", "");
        map.put("signVer", "5.5.153");
        map.put("appType", "ios");
        map.put("signType", user.getAppType());
        log.info("打卡参数:{}", map.toString());
        String body = HttpRequest.post("http://erp.kqgeo.com:889/kqerp_user_Kaoqin_onPositionSign.action?kqtime=" + System.currentTimeMillis())
                .cookie(cookies).form(map).timeout(20000).execute().body();
        log.info("打卡结果===:{}", body);
        return body;
    }
}
